//
//  CFileMgr.m
//  wanzhaoIM
//
//  Created by liuyuantao on 15/5/27.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "CFileMgr.h"
#import "FMDB.h"
#import "CFolderMgr.h"
#import "MicroDef.h"
#import "CFileModel.h"
@implementation CFileMgr
//插入文件
-(BOOL)insertFile:(CFileModel *)fileModel
{
    if(fileModel == nil)
    {
        return NO;
    }
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"INSERT INTO FILEMANAGER(LocalFilePath, FileSize, DownloadSize, FileName, LocalCacheName, CreateDate, MessageIndex, GroupId,serverFileName, fromUserId,yuLiuZiduan1,yuLiuZiduan2,yuLiuZiduan3) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ? ,?, ?, ? ,?)", M_STOREVALUE(fileModel.LocalFilePath), @(fileModel.FileSize), @(fileModel.DownloadSize), M_STOREVALUE(fileModel.FileName), M_STOREVALUE(fileModel.LocalCacheName), M_STOREVALUE(fileModel.CreateDate),  @(fileModel.MessageIndex), M_STOREVALUE(fileModel.GroupId),M_STOREVALUE(fileModel.serverFileName),M_STOREVALUE(fileModel.fromUserId),M_STOREVALUE(fileModel.yuLiuZiduan1),@(fileModel.yuLiuZiduan2),M_STOREVALUE(fileModel.yuLiuZiduan3)];
    }];
    return ret;
}
//批量插入文件
-(BOOL)batchInsertFile:(NSArray *)fileList
{
    if(fileList == nil || [fileList count] <= 0)
    {
        return NO;
    }
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for(CFileModel *fileModel in fileList)
        {
             ret = [db executeUpdate:@"INSERT INTO FILEMANAGER(LocalFilePath, FileSize, DownloadSize, FileName, LocalCacheName, CreateDate, MessageIndex, GroupId,serverFileName, fromUserId,yuLiuZiduan1,yuLiuZiduan2,yuLiuZiduan3) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ? ,?, ?, ? ,?)", M_STOREVALUE(fileModel.LocalFilePath), @(fileModel.FileSize), @(fileModel.DownloadSize), M_STOREVALUE(fileModel.FileName), M_STOREVALUE(fileModel.LocalCacheName), M_STOREVALUE(fileModel.CreateDate),  @(fileModel.MessageIndex), M_STOREVALUE(fileModel.GroupId),M_STOREVALUE(fileModel.serverFileName),M_STOREVALUE(fileModel.fromUserId),M_STOREVALUE(fileModel.yuLiuZiduan1),@(fileModel.yuLiuZiduan2),M_STOREVALUE(fileModel.yuLiuZiduan3)];
            
            if(!ret)
            {
                *rollback = YES;
                break;
            }
        }

    }];
    return ret;
}
//删除文件
-(BOOL)delFiles:(CFileModel *)fileModel
{
    if(fileModel == nil)
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"delete from FILEMANAGER where identifer = ?", @(fileModel.FileIndex)];
    }];
    return ret;
}
//通过组id删除文件
-(BOOL)delFilesByGroupId:(NSString *)groupId
{
    if(M_ISSTRNULL(groupId))
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"delete from FILEMANAGER where GroupId = ?", M_STOREVALUE(groupId)];
    }];
    return ret;
}
//更新文件
-(BOOL)updateFile:(CFileModel *)fileModel
{
    if(fileModel == nil)
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE FILEMANAGER SET LocalFilePath = ?, FileSize = ?, DownloadSize = ?, FileName = ?, LocalCacheName = ?, CreateDate = ?, MessageIndex = ?, GroupId = ?, serverFileName = ?, fromUserId = ? where identifer = ?", M_STOREVALUE(fileModel.LocalFilePath), @(fileModel.FileSize), @(fileModel.DownloadSize), M_STOREVALUE(fileModel.FileName), M_STOREVALUE(fileModel.LocalCacheName), M_STOREVALUE(fileModel.CreateDate), @(fileModel.MessageIndex), M_STOREVALUE(fileModel.GroupId), M_STOREVALUE(fileModel.serverFileName), M_STOREVALUE(fileModel.fromUserId), @(fileModel.FileIndex)];
        
    }];
    return ret;
}
//通过组id加载文件
-(NSArray *)loadFilesByGroupId:(NSString *)groupId
{
    if(M_ISSTRNULL(groupId))
    {
        return nil;
    }
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    NSString *sql = nil;
//    sql = [NSString stringWithFormat:@"select * from FILEMANAGER where GroupId = \'%@\'", groupId ];
//   按照日期降序排列
    sql = [NSString stringWithFormat:@"select * from FILEMANAGER where  GroupId = \'%@\' order by CreateDate desc", groupId ];
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CFileModel *fileModel = [[CFileModel alloc] init];
            fileModel.FileIndex = [result longLongIntForColumn:@"identifer"];
            fileModel.LocalFilePath = [result stringForColumn:@"LocalFilePath"];
            fileModel.FileSize = [result longLongIntForColumn:@"FileSize"];
            fileModel.DownloadSize = [result longLongIntForColumn:@"DownloadSize"];
            fileModel.FileName = [result stringForColumn:@"FileName"];
            fileModel.LocalCacheName = [result stringForColumn:@"LocalCacheName"];
            fileModel.CreateDate = [result stringForColumn:@"CreateDate"];
            fileModel.MessageIndex = [result longLongIntForColumn:@"MessageIndex"];
            fileModel.GroupId = [result stringForColumn:@"GroupId"];
            fileModel.fromUserId = [result stringForColumn:@"fromUserId"];
            fileModel.serverFileName = [result stringForColumn:@"serverFileName"];
            [array addObject:fileModel];
            fileModel = nil;
        }
        [result close];
    }];
    return array;
}

@end
